package com.zxl.live.screen.screen.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.zxl.live.screen.R;
import com.zxl.live.screen.screen.ui.activity.SuiScreenDetailActivity;
import com.zxl.live.screen.screen.ui.helper.data.SuiScreenDataHelper;
import com.zxl.live.tools.base.BaseAppCompatFragment;
import com.zxl.live.tools.track.StatisticsConstant;

/**
 * Created by Administrator on 2018/1/9.
 */

public class SuiScreenFragment extends BaseAppCompatFragment {

    private SuiScreenDataHelper.DataType mDataType;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_sui_screen, container, false);
    }

    public void setDataType(SuiScreenDataHelper.DataType mDataType) {
        this.mDataType = mDataType;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        GridView gridView = (GridView) view.findViewById(R.id.picture_select);
        gridView.setAdapter(new SuiScreenAdapter());
    }

    class SuiScreenAdapter extends BaseAdapter implements View.OnClickListener {

        @Override
        public int getCount() {
            return mDataType == null ? 0 : mDataType.getSize();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.adapter_sui_screen, parent, false);
                convertView.setOnClickListener(this);
            }
            ImageView imageView = (ImageView) convertView.findViewById(R.id.picture_item);
            Glide.with(getContext()).load(SuiScreenDataHelper.getMinImage(mDataType, position)).placeholder(R.drawable.wallpaper_loading).into(imageView);
            convertView.setTag(position);
            return convertView;
        }

        @Override
        public void onClick(View v) {
            int position = Integer.parseInt(v.getTag().toString());
            mDataType.upload(getContext(), StatisticsConstant.Event.CLICK, position + "");
            SuiScreenDetailActivity.startSuiScreenDetail(getContext(), mDataType, position);
        }
    }


}
